﻿using System;
using System.Collections;
using UnityEngine;
public class HeroSystem:Singleton<HeroSystem>
{
    [field:SerializeField] public HeroView HeroView { get; set; }

    private void OnEnable()
    {
        ActionSystem.AttachPerformer<HeroTurnGA>(HeroTurnPreform);
        // 订阅敌人回合事件
        ActionSystem.SubScribeReaction<EnemyTurnGA>(EnemyTurnPreReaction, ReactionTiming.PRE);
       // ActionSystem.SubScribeReaction<EnemyTurnGA>(EnemyTurnPostReaction, ReactionTiming.POST);
    }



    private void OnDisable()
    {
        ActionSystem.DetachPerformer<HeroTurnGA>();

        ActionSystem.UnsubScribeReaction<EnemyTurnGA>(EnemyTurnPreReaction, ReactionTiming.PRE);
      //  ActionSystem.UnsubScribeReaction<EnemyTurnGA>(EnemyTurnPostReaction, ReactionTiming.POST);
    }

    public void SetUp(HeroData data)
    {
        HeroView.SetUp(data);
    }
    #region 回合事件处理
    /// <summary>
    /// 敌人回合开始前的反应（弃掉所有手牌）
    /// </summary>
    private void EnemyTurnPreReaction(EnemyTurnGA enemyTurnGA)
    {
        // 创建并触发弃牌动作
        DiscardAllCardsGA discardAllCardsGA = new DiscardAllCardsGA();
        ActionSystem.instance.AddReaction(discardAllCardsGA);
    }

    /// <summary>
    /// 敌人回合结束后的反应（抽5张牌）
    /// </summary>
    //private void EnemyTurnPostReaction(EnemyTurnGA enemyTurnGA)
    //{
   
    //    int burnStacks = HeroView.GetStatusEffectStackCount(StatusEffectType.BURN);
    //    if(burnStacks > 0)
    //    {
    //        ApplyBurnGA applyBurnGA = new ApplyBurnGA(burnStacks, HeroView);
    //        ActionSystem.instance.AddReaction(applyBurnGA);
    //    }

    //    // 创建并触发抽牌动作
    //  //  ActionSystem.instance.AddReaction(new HeroTurnGA());
    //}

    private IEnumerator HeroTurnPreform(HeroTurnGA gA)
    {
        HeroState();
        ActionSystem.instance.AddReaction(new WaveGA("英雄回合"));
        DrawCardGA drawCardGa = new(5);
        ActionSystem.instance.AddReaction(drawCardGa);
        yield return null;

    }
    void HeroState()
    {
        int burnStacks = HeroView.GetStatusEffectStackCount(StatusEffectType.BURN);
        if (burnStacks > 0)
        {
            ApplyBurnGA applyBurnGA = new ApplyBurnGA(burnStacks, HeroView);
            ActionSystem.instance.AddReaction(applyBurnGA);
        }
    }
    #endregion
}
